<?php
/**
 * RedCross Application
 *
 * @copyright Copyright (c) 2010, Kristian Stokke Tryggestad, Rune Vikestad, Andreas Flaten Wist, Simen Nicolai Echholt, Ole Henrik Jahren
 */


class RedCross_Acl_Assert_IsShiftLeader implements Zend_Acl_Assert_Interface
{
	/**
     * @param Zend_Acl $acl
     * @param Zend_Acl_Role_Interface $role
     * @param Zend_Acl_Resource_Interface $resource
     * @param unknown_type $privilege
     */
    public function assert(Zend_Acl $acl, Zend_Acl_Role_Interface $user = null, Zend_Acl_Resource_Interface $shift = null, $privilege = null)
    {
        if ($user->getRoleId() == RedCross_Acl::GUEST_NAME) {
        	return false;
        }

        if (!$user instanceof Core_Model_User) {
        	throw new Exception(__CLASS__ . '::' . __METHOD__ . ' expects the role to be an instance of Core_Model_User');
        }
        if (!$shift instanceof Core_Model_Shift) {
        	throw new Exception(__CLASS__ . '::' . __METHOD__ . ' expects the resource to be an instance of Core_Model_Shift');;
        }

        if ($user->id == null || $shift->id == null) {
        	throw new Exception(__CLASS__ . '::' . __METHOD__ . ' expects the given objects to have valid IDs');
        }

        // Allow hvis user er admin
        if ($acl->isAllowed($user, 'service:shiftRole', $privilege)) {
        	return true;
        }

        // Allow hvis user er shift leader
        foreach ($shift->Roles as $role) {
            if ($role->member_id == $user->id && $role->leader === true) {
                return true;
            }
        }
        return false;
    }


}